﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloLLLLL.Blog.Common
{
   public class GetOs
    {
        public static string GetOSVersion(string userAgent)
        {
            //UserAgent   
           

            var osVersion = "未知";
            if (userAgent.Contains("NT 10.0"))
            {
                osVersion = "Windows 10";
            }
           else  if (userAgent.Contains("NT 6.1"))
            {
                osVersion = "Windows 7";
            }
            else if (userAgent.Contains("NT 6.0"))
            {
                osVersion = "Windows Vista/Server 2008";
            }
            else if (userAgent.Contains("NT 5.2"))
            {
                osVersion = "Windows Server 2003";
            }
            else if (userAgent.Contains("NT 5.1"))
            {
                osVersion = "Windows XP";
            }
            else if (userAgent.Contains("NT 5"))
            {
                osVersion = "Windows 2000";
            }
            else if (userAgent.Contains("NT 4"))
            {
                osVersion = "Windows NT4";
            }
            else if (userAgent.Contains("Me"))
            {
                osVersion = "Windows Me";
            }
            else if (userAgent.Contains("98"))
            {
                osVersion = "Windows 98";
            }
            else if (userAgent.Contains("95"))
            {
                osVersion = "Windows 95";
            }
            else if (userAgent.Contains("Mac"))
            {
                osVersion = "Mac";
            }
            else if (userAgent.Contains("Unix"))
            {
                osVersion = "UNIX";
            }
            else if (userAgent.Contains("Linux"))
            {
                osVersion = "Linux";
            }
            else if (userAgent.Contains("SunOS"))
            {
                osVersion = "SunOS";
            }
            else if (userAgent.ToLower().IndexOf("mac") >= 0)
            {
                osVersion = "Mac";
            }
            else if (userAgent.ToLower().IndexOf("x11") >= 0)
            {
                osVersion = "Unix";
            }
            else if (userAgent.ToLower().IndexOf("android") >= 0)
            {
                osVersion = "Android";
            }
            else if (userAgent.ToLower().IndexOf("iphone") >= 0)
            {
                osVersion = "IPhone";
            }
            String user = userAgent.ToLower();
            String browser = "";

            if (user.Contains("edge"))
            {
                browser = (userAgent.Substring(userAgent.IndexOf("Edge")).Split(new char[1] { ' ' })[0]).Replace("/", "-");
            }
            else if (user.Contains("msie"))
            {
                String substring = userAgent.Substring(userAgent.IndexOf("MSIE")).Split(new char[1] {';'})[0];
                browser = substring.Split(new char[1] { ' ' })[0].Replace("MSIE", "IE") + "-" + substring.Split(new char[1] { ' ' })[1];
            }
            else if (user.Contains("safari") && user.Contains("version"))
            {
                browser = (userAgent.Substring(userAgent.IndexOf("Safari")).Split(new char[1] { ' ' })[0]).Split(new char[1] { '/' })[0]
                        + "-" + (userAgent.Substring(userAgent.IndexOf("Version")).Split(new char[1] { ' ' })[0]).Split(new char[1] { '/' })[1];
            }
            else if (user.Contains("opr") || user.Contains("opera"))
            {
                if (user.Contains("opera"))
                {
                    browser = (userAgent.Substring(userAgent.IndexOf("Opera")).Split(new char[1] { ' ' })[0]).Split(new char[1] { '/' })[0]
                            + "-" + (userAgent.Substring(userAgent.IndexOf("Version")).Split(new char[1] { ' ' })[0]).Split(new char[1] { '/' })[1];
                }
                else if (user.Contains("opr"))
                {
                    browser = ((userAgent.Substring(userAgent.IndexOf("OPR")).Split(new char[1] { ' ' })[0]).Replace("/", "-"))
                            .Replace("OPR", "Opera");
                }

            }
            else if (user.Contains("chrome"))
            {
                browser = (userAgent.Substring(userAgent.IndexOf("Chrome")).Split(new char[1] { ' ' })[0]).Replace("/", "-");
            }
            else if ((user.IndexOf("mozilla/7.0") > -1) || (user.IndexOf("netscape6") != -1) ||
                  (user.IndexOf("mozilla/4.7") != -1) || (user.IndexOf("mozilla/4.78") != -1) ||
                  (user.IndexOf("mozilla/4.08") != -1) || (user.IndexOf("mozilla/3") != -1))
            {
                browser = "Netscape-?";

            }
            else if (user.Contains("firefox"))
            {
                browser = (userAgent.Substring(userAgent.IndexOf("Firefox")).Split(new char[1] { ' ' })[0]).Replace("/", "-");
            }
            else if (user.Contains("rv"))
            {
                String IEVersion = (userAgent.Substring(userAgent.IndexOf("rv")).Split(new char[1] { ' ' })[0]).Replace("rv:", "-");
                browser = "IE" + IEVersion.Substring(0, IEVersion.Length - 1);
            }
            else
            {
                browser = "未知浏览器";
            }
            return osVersion+"--"+browser;
        }
    }
}
